// 业务埋点使用功能的工具类
//1.天启埋点sdk
import {
  UBT
} from '../thridLib/wechat.js';
let en_js = require('/environment.js');
let user_js = require('/user.js');

UBT.config({
  env: en_js.ubtCodeEnvKey,
  debug: en_js.ubtCodeEnvKey !== 'pro', // 调试模式，可看日志，详情 5.1.1
  categoryId: 'evbike', // 若是多业务线协作，小程序需额外在 Page 文件中配置，详情 5.1.2（v1.2）
  enableApm: false, // APM性能采集开关，若不需要需指定 false，如需采集，需配合灰度开关，详情查看顶部 APM SDK（v1.2）
  enableUbt: true,
});

UBT.bindData({
  appVersion: en_js.ubtAppVersionKey,
  releaseVersion: en_js.ubtReleaseVersionKey,
  appName: 'youxuan',
  userGuid: () => {
    const userInfo = user_js.userInfo();
    const {
      openId
    } = (userInfo ? userInfo : {});
    return openId;
  },
  cityCode: () => {
    const {
      accurateCityCode
    } = {};
    return accurateCityCode;
  },
  adCode: () => {
    const {
      accurateAdCode
    } = {};
    return accurateAdCode;
  },
  longitude: () => {
    const userInfo = user_js.userInfo();
    const {
      geolat
    } = (userInfo ? userInfo : {});
    return geolat;
  },
  latitude: () => {
    const userInfo = user_js.userInfo();
    const {
      geolon
    } = (userInfo ? userInfo : {});
    return geolon;
  },
});

// 页面进入埋点
// pageId（必填）：页面 ID，英文
// categoryId（非必填）：业务 ID，英文
// businessInfo：业务参数 json，flagType、flagValue 等封装在这里面，不需要格式化成 string
function myPageView(pageId, businessInfo) {
  const userInfo = user_js.userInfo();
  var tmpInfo = businessInfo;
  if (!tmpInfo) {
    tmpInfo = {};
  }
  if (userInfo && userInfo.ztd && userInfo.ztd.id) {
    tmpInfo.shopId = userInfo.ztd.id;
  }
  let obj = {
    pageId: pageId,
    businessInfo: tmpInfo
  }
  UBT.pageView(obj);
}

// 页面退出埋点
function myPageViewOut(pageId, businessInfo) {
  let obj = {
    pageId: pageId,
    businessInfo: businessInfo
  }
  UBT.pageViewOut(obj);
}

// 按钮点击事件埋点
function myClickButton(pageId, buttonName, businessInfo) {
  const userInfo = user_js.userInfo();
  var tmpInfo = businessInfo;
  if (!tmpInfo) {
    tmpInfo = {};
  }
  if (userInfo && userInfo.ztd && userInfo.ztd.id) {
    tmpInfo.shopId = userInfo.ztd.id;
  }
  let obj = {
    pageId: pageId,
    buttonName: buttonName,
    businessInfo: tmpInfo
  }
  UBT.clickButton(obj);
}

module.exports = {
  myPageView: myPageView,
  myPageViewOut: myPageViewOut,
  myClickButton: myClickButton
}